# 同前一题
class Solution:
    def numberOfPairs(self, points: List[List[int]]) -> int:
        points.sort(key=lambda x: (-x[0], x[1])) # 第一个降序，第二个升序
        ans = 0
        i, j = 0, 1
        flag = False
        while i < len(points):
            flag = False
            if j == len(points):
                break
            if points[j][1] >= points[i][1]:
                for k in range(i + 1, j):
                    if points[j][0] <= points[k][0] <= points[i][0] and points[i][1] <= points[k][1] <= points[j][1]:
                        flag = True
                        break
                if not flag:
                    ans += 1
            j += 1
            if j == len(points):
                i += 1
                j = i + 1
        return ans
# 优化
class Solution:
    def numberOfPairs(self, points: List[List[int]]) -> int:
        points.sort(key=lambda p: (p[0], -p[1]))  # x 升序，y 降序
        ans = 0
        for i, (_, y1) in enumerate(points):
            max_y = -inf
            for (_, y2) in points[i + 1:]:
                if y1 >= y2 > max_y:
                    max_y = y2 # 优化
                    ans += 1
                if max_y == y1:  # 优化
                    break
        return ans